<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>添加地址</title>
    <link rel="stylesheet" type="text/css" href="../../../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="./res/area.css" />
    <style>
        input {
            text-align: right;
        }

        .aui-radio {
            width: 0.8rem;
            height: 0.8rem;
            margin-top: 0.2rem;
        }
    </style>
</head>

<body>
    <div id="app" class="aui-content aui-margin-b-15">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">姓名</div>
                    <div class="aui-list-item-input">
                        <input type="text" v-model="info.contact" placeholder="请输入姓名">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">所在地</div>
                    <div class="aui-list-item-input" @click="openCity()">
                        <input id="getArea" v-model="info.region" placeholder="请选择" style="width:100%" readonly/>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">详细地址</div>
                    <div class="aui-list-item-input">
                        <input type="text" v-model="info.address" placeholder="请输入详细的地址信息">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">手机号</div>
                    <div class="aui-list-item-input">
                        <input type="tel" v-model="info.contact_phone" placeholder="请输入手机号">
                    </div>
                </div>
            </li>


            <div style="padding:1rem 0.75rem;">
                <label><input class="aui-radio" type="checkbox" v-model="info.is_default" name="demo1" checked> 设为默认</label>
            </div>

            <div class="aui-bar-tab-item aui-padded-l-15 aui-padded-r-15 aui-padded-b-15" style="margin-top:2rem;">
                <div class="aui-btn aui-btn-block aui-btn-sm aui-btn-info" @click="submit()">保存</div>
            </div>
        </ul>

    </div>
</body>
<script src="../../../script/api.js" charset="utf-8"></script>
<script src="../../../script/aui/aui-tab.js" charset="utf-8"></script>
<script src="../../../script/common/jquery-3.3.1.min.js" charset="utf-8"></script>
<script src="../../../script/common/check.js" charset="utf-8"></script>
<script src="../../../script/common/common.js" charset="utf-8"></script>
<script src="../../../script/common/config.js" charset="utf-8"></script>
<script src="../../../script/common/vue.min.js" charset="utf-8"></script>
<script src="./res/area.js" charset="utf-8"></script>
<script type="text/javascript">
    apiready = function() {
        api.parseTapmode();
        vm.init();
    };

    var vm = new Vue({
        el: "#app",
        data: {
            userInfo: {},
            type: 'add',
            info: {
                user_id: '',
                region_provinceid: '',
                region_cityid: '',
                region_areaid: '',
                region: '',
                address: '',
                contact: '',
                contact_phone: '',
                is_default: 1
            }
        },
        methods: {
            init() {
                this.userInfo = get_local("userInfo");
                this.info.user_id = this.userInfo.user_id;
                this.type = api.pageParam.type;

                if (this.type === 'update') {
                    this.getAddrInfo();
                }
            },
            submit() {
                if (!is_define(this.info.contact)) {
                    $toast("请输入联系人");
                    return;
                } else if (!is_define(this.info.region)) {
                    $toast("请选择所在地");
                    return;
                } else if (!is_define(this.info.address)) {
                    $toast("请输入详细地址");
                    return;
                } else if (!is_define(this.info.contact_phone)) {
                    $toast("请输入联系人电话");
                    return;
                } else if (!checkMobile(this.info.contact_phone)) {
                    $toast("请输入正确的手机号");
                    return;
                }

                this.info.is_default = this.info.is_default?1:0;

                var that = this;
                var apiInfo = {
                    add: {
                        'apiurl': Config.api.UserAddressCreate,
                        'msg': '添加成功',
                    },
                    update: {
                        apiurl: Config.api.UserAddressUpdate,
                        msg: '修改成功',
                    }
                };
                _ajax(apiInfo[that.type]['apiurl'], that.info, function(ret) {
                    if (ret.code === 200) {
                        $toast(apiInfo[that.type]['msg']);
                        // 发送监听
                        set_sendEvent('updateAddr');
                        setTimeout(function() {
                            close_w();
                        }, 500);
                    }
                });
            },
            getAddrInfo() {
                var that = this;
                var data = {
                    user_id: that.userInfo.user_id,
                    address_id: api.pageParam.id
                };
                _ajax(Config.api.UserAddressGetDetail, data, function(ret) {
                    if (ret.code === 200) {
                        that.info = ret.data;
                    }
                });
            },
            selectAddr() {
                $('#getArea').getArea({
                    defaultArea: [18, 2, 3],
                    inpEle: '#getArea',
                    normalArea: true
                });
            },
            openCity() {
                var url = '../../city/city_win.html';
                api.openFrame({
                    name: 'mask',
                    url: url,
                    rect: {
                        x: 0,
                        y: api.winHeight - api.frameHeight,
                        w: 'auto',
                        h: 'auto'
                    },
                    bgColor: 'rgba(0,0,0,0.5)',
                    pageParam: {
                        name: api.winName,
                        frameName: api.frameName,
                    }
                });
            },
        }
    });

    function setCity(province, city, area) {
        vm.info.region_provinceid = province.code;
        vm.info.region_cityid = city.code;
        vm.info.region_areaid = area.code;

        vm.info.region = province.name + '-' + city.name + '-' + area.name
    }
</script>

</html>
